/*
 * # 应用所有数据库表:
 *  Authority - 组织与用户
 * 
 * # 描述:
 *  为应用提供组织、工作岗位、用户的管理功能，以及针对组织、部门、岗位和用户的权限分派和验证功能，同时提供用户登录、注销、导航等基础功能。
 * 
 * Copyright (c), 上海沃恩信息科技有限公司.
 * All rights reserved.
 * Licensed under BSD (https://www.pittypat.work/bsd.html).
 */

use "pittypat_app";
go

-- 确保架构存在
if schema_id('waln_authority') is null
	exec('create schema "waln_authority" authorization "dbo"');
go

/*
 * # 辅助函数:
 *  _pittypat_table_exists - 检查指定的表是否已经存在
 * 
 * # 描述:
 *  检查指定的表是否已经存在。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_table_exists"') and "type" in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	exec('create function "waln_authority"."_pittypat_table_exists"() returns int begin return 0; end');
go

alter function "waln_authority"."_pittypat_table_exists"(@tbl varchar(30))
returns bit
as
begin
	if @tbl is not null and object_id(N'"waln_authority".' + @tbl, N'U') is not null
		return 1;
	return 0;
end;
go

/*
 * # 辅助函数:
 *  _pittypat_column_exists - 检查指定的列是否已经存在
 * 
 * # 描述:
 *  检查指定的列是否已经存在。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_column_exists"') and "type" in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	exec('create function "waln_authority"."_pittypat_column_exists"() returns int begin return 0; end');
go

alter function "waln_authority"."_pittypat_column_exists"(@tbl varchar(30), @col varchar(30))
returns bit
as
begin
	if @tbl is not null and @col is not null and 
		exists (select * from "sys"."columns" where "object_id" = object_id(N'[waln_authority].' + @tbl, N'U') and "name" = @col)
		return 1;
	return 0;
end;
go

/*
 * # 辅助函数:
 *  _pittypat_index_exists - 检查指定的索引是否已经存在
 * 
 * # 描述:
 *  检查指定的索引是否已经存在。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_index_exists"') and "type" in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	exec('create function "waln_authority"."_pittypat_index_exists"() returns int begin return 0; end');
go

alter function "waln_authority"."_pittypat_index_exists"(@tbl varchar(30), @idx varchar(63))
returns bit
as
begin
	if @tbl is not null and @idx is not null and 
		exists (select * from "sys"."indexes" where "object_id" = object_id(N'"waln_authority".' + @tbl, N'U') and "name" = @idx)
		return 1;
	return 0;
end;
go

/*
 * # 数据库表 - Sql Server:
 *  Depts - 部门
 * 
 * # 描述:
 *  表示组织结构中的一个部门。组织结构图是一个倒立的树状结构。树根为根组织，其层级为 0，其他分别为 1、2...。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_authority"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_authority"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_authority"."_pittypat_table_exists"('"Depts"') = 0
	begin
		create table "waln_authority"."Depts"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"Id" uniqueidentifier not null, -- Id
			"Level" tinyint not null, -- 层级
			"ParentId" uniqueidentifier null, -- 上级部门的 Id
			"OrgId" uniqueidentifier not null, -- 根组织的 Id
			"Name" nvarchar(50) not null, -- 名称
			"Description" nvarchar(2000) null, -- 描述
			"CreateDate" date not null -- 设立日期
		)
		on "primary";

		-- 索引 K_Depts
		alter table "waln_authority"."Depts" add constraint
		"K_Depts" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Depts_Id
		create unique nonclustered index "U_Depts_Id"
		on "waln_authority"."Depts"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Depts_ParentId
		create nonclustered index "I_Depts_ParentId"
		on "waln_authority"."Depts"
		(
			"ParentId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 Id 
		if "waln_authority"."_pittypat_column_exists"('Depts', 'Id') = 0
			alter table "waln_authority"."Depts" add
			"Id" uniqueidentifier not null;
		else
			alter table "waln_authority"."Depts" alter column
			"Id" uniqueidentifier not null;

		-- 列 Level 
		if "waln_authority"."_pittypat_column_exists"('Depts', 'Level') = 0
			alter table "waln_authority"."Depts" add
			"Level" tinyint not null;
		else
			alter table "waln_authority"."Depts" alter column
			"Level" tinyint not null;

		-- 列 ParentId 
		if "waln_authority"."_pittypat_column_exists"('Depts', 'ParentId') = 0
			alter table "waln_authority"."Depts" add
			"ParentId" uniqueidentifier null;
		else
			alter table "waln_authority"."Depts" alter column
			"ParentId" uniqueidentifier null;

		-- 列 OrgId 
		if "waln_authority"."_pittypat_column_exists"('Depts', 'OrgId') = 0
			alter table "waln_authority"."Depts" add
			"OrgId" uniqueidentifier not null;
		else
			alter table "waln_authority"."Depts" alter column
			"OrgId" uniqueidentifier not null;

		-- 列 Name 
		if "waln_authority"."_pittypat_column_exists"('Depts', 'Name') = 0
			alter table "waln_authority"."Depts" add
			"Name" nvarchar(50) not null;
		else
			alter table "waln_authority"."Depts" alter column
			"Name" nvarchar(50) not null;

		-- 列 Description 
		if "waln_authority"."_pittypat_column_exists"('Depts', 'Description') = 0
			alter table "waln_authority"."Depts" add
			"Description" nvarchar(2000) null;
		else
			alter table "waln_authority"."Depts" alter column
			"Description" nvarchar(2000) null;

		-- 列 CreateDate 
		if "waln_authority"."_pittypat_column_exists"('Depts', 'CreateDate') = 0
			alter table "waln_authority"."Depts" add
			"CreateDate" date not null;
		else
			alter table "waln_authority"."Depts" alter column
			"CreateDate" date not null;

		-- 删除索引 U_Depts_Id
		if "waln_authority"."_pittypat_index_exists"('Depts', 'U_Depts_Id') = 1
			drop index "U_Depts_Id" on "waln_authority"."Depts";

		-- 创建索引 U_Depts_Id
		create unique nonclustered index "U_Depts_Id"
		on "waln_authority"."Depts"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Depts_ParentId
		if "waln_authority"."_pittypat_index_exists"('Depts', 'I_Depts_ParentId') = 1
			drop index "I_Depts_ParentId" on "waln_authority"."Depts";

		-- 创建索引 I_Depts_ParentId
		create nonclustered index "I_Depts_ParentId"
		on "waln_authority"."Depts"
		(
			"ParentId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_authority"."_pittypat_replace_table";
go

/*
	select
		"Id", -- Id
		"Level", -- 层级
		"ParentId", -- 上级部门的 Id
		"OrgId", -- 根组织的 Id
		"Name", -- 名称
		"Description", -- 描述
		"CreateDate"  -- 设立日期
	from
		"waln_authority"."Depts"
	where
		"Id" = @id;

	insert into "waln_authority"."Depts"
	(
		"Id", -- Id
		"Level", -- 层级
		"ParentId", -- 上级部门的 Id
		"OrgId", -- 根组织的 Id
		"Name", -- 名称
		"Description", -- 描述
		"CreateDate"  -- 设立日期
	)
	values
	(
		@id, -- Id
		@level, -- 层级
		@parentId, -- 上级部门的 Id
		@orgId, -- 根组织的 Id
		@name, -- 名称
		@description, -- 描述
		@createDate  -- 设立日期
	);

	update
		"waln_authority"."Depts"
	set
		"Id" = @id, -- Id
		"Level" = @level, -- 层级
		"ParentId" = @parentId, -- 上级部门的 Id
		"OrgId" = @orgId, -- 根组织的 Id
		"Name" = @name, -- 名称
		"Description" = @description, -- 描述
		"CreateDate" = @createDate  -- 设立日期
	where
		"Id" = @id;

	delete from
		"waln_authority"."Depts"
	where
		"Id" = @id;
*/

/*
 * # 数据库表 - Sql Server:
 *  Posts - 工作岗位
 * 
 * # 描述:
 *  表示在一个组织或部门中设立的工作岗位。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_authority"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_authority"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_authority"."_pittypat_table_exists"('"Posts"') = 0
	begin
		create table "waln_authority"."Posts"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"Id" uniqueidentifier not null, -- Id
			"DeptId" uniqueidentifier not null, -- 隶属部门的 Id
			"Name" nvarchar(50) not null, -- 岗位名称
			"Description" nvarchar(2000) null, -- 描述
			"CreateDate" date not null -- 设立日期
		)
		on "primary";

		-- 索引 K_Posts
		alter table "waln_authority"."Posts" add constraint
		"K_Posts" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Posts_Id
		create unique nonclustered index "U_Posts_Id"
		on "waln_authority"."Posts"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Posts_DeptId
		create nonclustered index "I_Posts_DeptId"
		on "waln_authority"."Posts"
		(
			"DeptId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 Id 
		if "waln_authority"."_pittypat_column_exists"('Posts', 'Id') = 0
			alter table "waln_authority"."Posts" add
			"Id" uniqueidentifier not null;
		else
			alter table "waln_authority"."Posts" alter column
			"Id" uniqueidentifier not null;

		-- 列 DeptId 
		if "waln_authority"."_pittypat_column_exists"('Posts', 'DeptId') = 0
			alter table "waln_authority"."Posts" add
			"DeptId" uniqueidentifier not null;
		else
			alter table "waln_authority"."Posts" alter column
			"DeptId" uniqueidentifier not null;

		-- 列 Name 
		if "waln_authority"."_pittypat_column_exists"('Posts', 'Name') = 0
			alter table "waln_authority"."Posts" add
			"Name" nvarchar(50) not null;
		else
			alter table "waln_authority"."Posts" alter column
			"Name" nvarchar(50) not null;

		-- 列 Description 
		if "waln_authority"."_pittypat_column_exists"('Posts', 'Description') = 0
			alter table "waln_authority"."Posts" add
			"Description" nvarchar(2000) null;
		else
			alter table "waln_authority"."Posts" alter column
			"Description" nvarchar(2000) null;

		-- 列 CreateDate 
		if "waln_authority"."_pittypat_column_exists"('Posts', 'CreateDate') = 0
			alter table "waln_authority"."Posts" add
			"CreateDate" date not null;
		else
			alter table "waln_authority"."Posts" alter column
			"CreateDate" date not null;

		-- 删除索引 U_Posts_Id
		if "waln_authority"."_pittypat_index_exists"('Posts', 'U_Posts_Id') = 1
			drop index "U_Posts_Id" on "waln_authority"."Posts";

		-- 创建索引 U_Posts_Id
		create unique nonclustered index "U_Posts_Id"
		on "waln_authority"."Posts"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Posts_DeptId
		if "waln_authority"."_pittypat_index_exists"('Posts', 'I_Posts_DeptId') = 1
			drop index "I_Posts_DeptId" on "waln_authority"."Posts";

		-- 创建索引 I_Posts_DeptId
		create nonclustered index "I_Posts_DeptId"
		on "waln_authority"."Posts"
		(
			"DeptId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_authority"."_pittypat_replace_table";
go

/*
	select
		"Id", -- Id
		"DeptId", -- 隶属部门的 Id
		"Name", -- 岗位名称
		"Description", -- 描述
		"CreateDate"  -- 设立日期
	from
		"waln_authority"."Posts"
	where
		"Id" = @id;

	insert into "waln_authority"."Posts"
	(
		"Id", -- Id
		"DeptId", -- 隶属部门的 Id
		"Name", -- 岗位名称
		"Description", -- 描述
		"CreateDate"  -- 设立日期
	)
	values
	(
		@id, -- Id
		@deptId, -- 隶属部门的 Id
		@name, -- 岗位名称
		@description, -- 描述
		@createDate  -- 设立日期
	);

	update
		"waln_authority"."Posts"
	set
		"Id" = @id, -- Id
		"DeptId" = @deptId, -- 隶属部门的 Id
		"Name" = @name, -- 岗位名称
		"Description" = @description, -- 描述
		"CreateDate" = @createDate  -- 设立日期
	where
		"Id" = @id;

	delete from
		"waln_authority"."Posts"
	where
		"Id" = @id;
*/

/*
 * # 数据库表 - Sql Server:
 *  Users - 用户
 * 
 * # 描述:
 *  表示一个可以登录和使用系统的用户。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_authority"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_authority"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_authority"."_pittypat_table_exists"('"Users"') = 0
	begin
		create table "waln_authority"."Users"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"Id" uniqueidentifier not null, -- Id
			"LoginName" nvarchar(50) not null, -- 登录名称
			"Password" binary(36) not null, -- 登录密码密文
			"No" varchar(20) null, -- 编号
			"Name" nvarchar(50) null, -- 用户名称
			"FaceUrl" nvarchar(200) null, -- 头像路径
			"Mobile" varchar(20) null, -- 手机号码
			"Email" varchar(50) null, -- 电子邮件地址
			"CreateTime" datetime2 not null, -- 创建时间
			"LockTime" datetime2 null, -- 锁定时间
			"LoginTime" datetime2 null, -- 登录时间
			"Token" binary(36) null, -- 用户令牌
			"TokenExpireTime" datetime2 null, -- 令牌有效期
			"LoginHomeUrl" nvarchar(200) null, -- 登录后首页
			"LoginHomeDescription" nvarchar(100) null -- 登录后首页说明
		)
		on "primary";

		-- 索引 K_Users
		alter table "waln_authority"."Users" add constraint
		"K_Users" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Users_Id
		create unique nonclustered index "U_Users_Id"
		on "waln_authority"."Users"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Users_LoginName
		create unique nonclustered index "U_Users_LoginName"
		on "waln_authority"."Users"
		(
			"LoginName" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Users_No
		create nonclustered index "I_Users_No"
		on "waln_authority"."Users"
		(
			"No" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Users_Mobile
		create nonclustered index "I_Users_Mobile"
		on "waln_authority"."Users"
		(
			"Mobile" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Users_Email
		create nonclustered index "I_Users_Email"
		on "waln_authority"."Users"
		(
			"Email" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Users_Token
		create nonclustered index "I_Users_Token"
		on "waln_authority"."Users"
		(
			"Token" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 Id 
		if "waln_authority"."_pittypat_column_exists"('Users', 'Id') = 0
			alter table "waln_authority"."Users" add
			"Id" uniqueidentifier not null;
		else
			alter table "waln_authority"."Users" alter column
			"Id" uniqueidentifier not null;

		-- 列 LoginName 
		if "waln_authority"."_pittypat_column_exists"('Users', 'LoginName') = 0
			alter table "waln_authority"."Users" add
			"LoginName" nvarchar(50) not null;
		else
			alter table "waln_authority"."Users" alter column
			"LoginName" nvarchar(50) not null;

		-- 列 Password 
		if "waln_authority"."_pittypat_column_exists"('Users', 'Password') = 0
			alter table "waln_authority"."Users" add
			"Password" binary(36) not null;
		else
			alter table "waln_authority"."Users" alter column
			"Password" binary(36) not null;

		-- 列 No 
		if "waln_authority"."_pittypat_column_exists"('Users', 'No') = 0
			alter table "waln_authority"."Users" add
			"No" varchar(20) null;
		else
			alter table "waln_authority"."Users" alter column
			"No" varchar(20) null;

		-- 列 Name 
		if "waln_authority"."_pittypat_column_exists"('Users', 'Name') = 0
			alter table "waln_authority"."Users" add
			"Name" nvarchar(50) null;
		else
			alter table "waln_authority"."Users" alter column
			"Name" nvarchar(50) null;

		-- 列 FaceUrl 
		if "waln_authority"."_pittypat_column_exists"('Users', 'FaceUrl') = 0
			alter table "waln_authority"."Users" add
			"FaceUrl" nvarchar(200) null;
		else
			alter table "waln_authority"."Users" alter column
			"FaceUrl" nvarchar(200) null;

		-- 列 Mobile 
		if "waln_authority"."_pittypat_column_exists"('Users', 'Mobile') = 0
			alter table "waln_authority"."Users" add
			"Mobile" varchar(20) null;
		else
			alter table "waln_authority"."Users" alter column
			"Mobile" varchar(20) null;

		-- 列 Email 
		if "waln_authority"."_pittypat_column_exists"('Users', 'Email') = 0
			alter table "waln_authority"."Users" add
			"Email" varchar(50) null;
		else
			alter table "waln_authority"."Users" alter column
			"Email" varchar(50) null;

		-- 列 CreateTime 
		if "waln_authority"."_pittypat_column_exists"('Users', 'CreateTime') = 0
			alter table "waln_authority"."Users" add
			"CreateTime" datetime2 not null;
		else
			alter table "waln_authority"."Users" alter column
			"CreateTime" datetime2 not null;

		-- 列 LockTime 
		if "waln_authority"."_pittypat_column_exists"('Users', 'LockTime') = 0
			alter table "waln_authority"."Users" add
			"LockTime" datetime2 null;
		else
			alter table "waln_authority"."Users" alter column
			"LockTime" datetime2 null;

		-- 列 LoginTime 
		if "waln_authority"."_pittypat_column_exists"('Users', 'LoginTime') = 0
			alter table "waln_authority"."Users" add
			"LoginTime" datetime2 null;
		else
			alter table "waln_authority"."Users" alter column
			"LoginTime" datetime2 null;

		-- 列 Token 
		if "waln_authority"."_pittypat_column_exists"('Users', 'Token') = 0
			alter table "waln_authority"."Users" add
			"Token" binary(36) null;
		else
			alter table "waln_authority"."Users" alter column
			"Token" binary(36) null;

		-- 列 TokenExpireTime 
		if "waln_authority"."_pittypat_column_exists"('Users', 'TokenExpireTime') = 0
			alter table "waln_authority"."Users" add
			"TokenExpireTime" datetime2 null;
		else
			alter table "waln_authority"."Users" alter column
			"TokenExpireTime" datetime2 null;

		-- 列 LoginHomeUrl 
		if "waln_authority"."_pittypat_column_exists"('Users', 'LoginHomeUrl') = 0
			alter table "waln_authority"."Users" add
			"LoginHomeUrl" nvarchar(200) null;
		else
			alter table "waln_authority"."Users" alter column
			"LoginHomeUrl" nvarchar(200) null;

		-- 列 LoginHomeDescription 
		if "waln_authority"."_pittypat_column_exists"('Users', 'LoginHomeDescription') = 0
			alter table "waln_authority"."Users" add
			"LoginHomeDescription" nvarchar(100) null;
		else
			alter table "waln_authority"."Users" alter column
			"LoginHomeDescription" nvarchar(100) null;

		-- 删除索引 U_Users_Id
		if "waln_authority"."_pittypat_index_exists"('Users', 'U_Users_Id') = 1
			drop index "U_Users_Id" on "waln_authority"."Users";

		-- 创建索引 U_Users_Id
		create unique nonclustered index "U_Users_Id"
		on "waln_authority"."Users"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 U_Users_LoginName
		if "waln_authority"."_pittypat_index_exists"('Users', 'U_Users_LoginName') = 1
			drop index "U_Users_LoginName" on "waln_authority"."Users";

		-- 创建索引 U_Users_LoginName
		create unique nonclustered index "U_Users_LoginName"
		on "waln_authority"."Users"
		(
			"LoginName" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Users_No
		if "waln_authority"."_pittypat_index_exists"('Users', 'I_Users_No') = 1
			drop index "I_Users_No" on "waln_authority"."Users";

		-- 创建索引 I_Users_No
		create nonclustered index "I_Users_No"
		on "waln_authority"."Users"
		(
			"No" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Users_Mobile
		if "waln_authority"."_pittypat_index_exists"('Users', 'I_Users_Mobile') = 1
			drop index "I_Users_Mobile" on "waln_authority"."Users";

		-- 创建索引 I_Users_Mobile
		create nonclustered index "I_Users_Mobile"
		on "waln_authority"."Users"
		(
			"Mobile" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Users_Email
		if "waln_authority"."_pittypat_index_exists"('Users', 'I_Users_Email') = 1
			drop index "I_Users_Email" on "waln_authority"."Users";

		-- 创建索引 I_Users_Email
		create nonclustered index "I_Users_Email"
		on "waln_authority"."Users"
		(
			"Email" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Users_Token
		if "waln_authority"."_pittypat_index_exists"('Users', 'I_Users_Token') = 1
			drop index "I_Users_Token" on "waln_authority"."Users";

		-- 创建索引 I_Users_Token
		create nonclustered index "I_Users_Token"
		on "waln_authority"."Users"
		(
			"Token" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_authority"."_pittypat_replace_table";
go

/*
	select
		"Id", -- Id
		"LoginName", -- 登录名称
		"Password", -- 登录密码密文
		"No", -- 编号
		"Name", -- 用户名称
		"FaceUrl", -- 头像路径
		"Mobile", -- 手机号码
		"Email", -- 电子邮件地址
		"CreateTime", -- 创建时间
		"LockTime", -- 锁定时间
		"LoginTime", -- 登录时间
		"Token", -- 用户令牌
		"TokenExpireTime", -- 令牌有效期
		"LoginHomeUrl", -- 登录后首页
		"LoginHomeDescription"  -- 登录后首页说明
	from
		"waln_authority"."Users"
	where
		"Id" = @id;

	insert into "waln_authority"."Users"
	(
		"Id", -- Id
		"LoginName", -- 登录名称
		"Password", -- 登录密码密文
		"No", -- 编号
		"Name", -- 用户名称
		"FaceUrl", -- 头像路径
		"Mobile", -- 手机号码
		"Email", -- 电子邮件地址
		"CreateTime", -- 创建时间
		"LockTime", -- 锁定时间
		"LoginTime", -- 登录时间
		"Token", -- 用户令牌
		"TokenExpireTime", -- 令牌有效期
		"LoginHomeUrl", -- 登录后首页
		"LoginHomeDescription"  -- 登录后首页说明
	)
	values
	(
		@id, -- Id
		@loginName, -- 登录名称
		@password, -- 登录密码密文
		@no, -- 编号
		@name, -- 用户名称
		@faceUrl, -- 头像路径
		@mobile, -- 手机号码
		@email, -- 电子邮件地址
		@createTime, -- 创建时间
		@lockTime, -- 锁定时间
		@loginTime, -- 登录时间
		@token, -- 用户令牌
		@tokenExpireTime, -- 令牌有效期
		@loginHomeUrl, -- 登录后首页
		@loginHomeDescription  -- 登录后首页说明
	);

	update
		"waln_authority"."Users"
	set
		"Id" = @id, -- Id
		"LoginName" = @loginName, -- 登录名称
		"Password" = @password, -- 登录密码密文
		"No" = @no, -- 编号
		"Name" = @name, -- 用户名称
		"FaceUrl" = @faceUrl, -- 头像路径
		"Mobile" = @mobile, -- 手机号码
		"Email" = @email, -- 电子邮件地址
		"CreateTime" = @createTime, -- 创建时间
		"LockTime" = @lockTime, -- 锁定时间
		"LoginTime" = @loginTime, -- 登录时间
		"Token" = @token, -- 用户令牌
		"TokenExpireTime" = @tokenExpireTime, -- 令牌有效期
		"LoginHomeUrl" = @loginHomeUrl, -- 登录后首页
		"LoginHomeDescription" = @loginHomeDescription  -- 登录后首页说明
	where
		"Id" = @id;

	delete from
		"waln_authority"."Users"
	where
		"Id" = @id;
*/

/*
 * # 数据库表 - Sql Server:
 *  UserPosts - 用户岗位
 * 
 * # 描述:
 *  表示用户的一个或多个工作岗位，定义了用户的权限。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_authority"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_authority"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_authority"."_pittypat_table_exists"('"UserPosts"') = 0
	begin
		create table "waln_authority"."UserPosts"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"UserId" uniqueidentifier not null, -- 用户的 Id
			"PostId" uniqueidentifier null, -- 工作岗位的 Id
			"DeptId" uniqueidentifier null, -- 隶属部门的 Id
			"OrgId" uniqueidentifier not null, -- 根组织的 Id
			"CreateTime" datetime2 not null, -- 分配时间
			"Description" nvarchar(2000) null -- 描述
		)
		on "primary";

		-- 索引 K_UserPosts
		alter table "waln_authority"."UserPosts" add constraint
		"K_UserPosts" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_UserPosts_UserId
		create nonclustered index "I_UserPosts_UserId"
		on "waln_authority"."UserPosts"
		(
			"UserId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_UserPosts_PostId
		create nonclustered index "I_UserPosts_PostId"
		on "waln_authority"."UserPosts"
		(
			"PostId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_UserPosts_DeptId
		create nonclustered index "I_UserPosts_DeptId"
		on "waln_authority"."UserPosts"
		(
			"DeptId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_UserPosts_OrgId
		create nonclustered index "I_UserPosts_OrgId"
		on "waln_authority"."UserPosts"
		(
			"OrgId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 UserId 
		if "waln_authority"."_pittypat_column_exists"('UserPosts', 'UserId') = 0
			alter table "waln_authority"."UserPosts" add
			"UserId" uniqueidentifier not null;
		else
			alter table "waln_authority"."UserPosts" alter column
			"UserId" uniqueidentifier not null;

		-- 列 PostId 
		if "waln_authority"."_pittypat_column_exists"('UserPosts', 'PostId') = 0
			alter table "waln_authority"."UserPosts" add
			"PostId" uniqueidentifier null;
		else
			alter table "waln_authority"."UserPosts" alter column
			"PostId" uniqueidentifier null;

		-- 列 DeptId 
		if "waln_authority"."_pittypat_column_exists"('UserPosts', 'DeptId') = 0
			alter table "waln_authority"."UserPosts" add
			"DeptId" uniqueidentifier null;
		else
			alter table "waln_authority"."UserPosts" alter column
			"DeptId" uniqueidentifier null;

		-- 列 OrgId 
		if "waln_authority"."_pittypat_column_exists"('UserPosts', 'OrgId') = 0
			alter table "waln_authority"."UserPosts" add
			"OrgId" uniqueidentifier not null;
		else
			alter table "waln_authority"."UserPosts" alter column
			"OrgId" uniqueidentifier not null;

		-- 列 CreateTime 
		if "waln_authority"."_pittypat_column_exists"('UserPosts', 'CreateTime') = 0
			alter table "waln_authority"."UserPosts" add
			"CreateTime" datetime2 not null;
		else
			alter table "waln_authority"."UserPosts" alter column
			"CreateTime" datetime2 not null;

		-- 列 Description 
		if "waln_authority"."_pittypat_column_exists"('UserPosts', 'Description') = 0
			alter table "waln_authority"."UserPosts" add
			"Description" nvarchar(2000) null;
		else
			alter table "waln_authority"."UserPosts" alter column
			"Description" nvarchar(2000) null;

		-- 删除索引 I_UserPosts_UserId
		if "waln_authority"."_pittypat_index_exists"('UserPosts', 'I_UserPosts_UserId') = 1
			drop index "I_UserPosts_UserId" on "waln_authority"."UserPosts";

		-- 创建索引 I_UserPosts_UserId
		create nonclustered index "I_UserPosts_UserId"
		on "waln_authority"."UserPosts"
		(
			"UserId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_UserPosts_PostId
		if "waln_authority"."_pittypat_index_exists"('UserPosts', 'I_UserPosts_PostId') = 1
			drop index "I_UserPosts_PostId" on "waln_authority"."UserPosts";

		-- 创建索引 I_UserPosts_PostId
		create nonclustered index "I_UserPosts_PostId"
		on "waln_authority"."UserPosts"
		(
			"PostId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_UserPosts_DeptId
		if "waln_authority"."_pittypat_index_exists"('UserPosts', 'I_UserPosts_DeptId') = 1
			drop index "I_UserPosts_DeptId" on "waln_authority"."UserPosts";

		-- 创建索引 I_UserPosts_DeptId
		create nonclustered index "I_UserPosts_DeptId"
		on "waln_authority"."UserPosts"
		(
			"DeptId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_UserPosts_OrgId
		if "waln_authority"."_pittypat_index_exists"('UserPosts', 'I_UserPosts_OrgId') = 1
			drop index "I_UserPosts_OrgId" on "waln_authority"."UserPosts";

		-- 创建索引 I_UserPosts_OrgId
		create nonclustered index "I_UserPosts_OrgId"
		on "waln_authority"."UserPosts"
		(
			"OrgId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_authority"."_pittypat_replace_table";
go

/*
	select
		"UserId", -- 用户的 Id
		"PostId", -- 工作岗位的 Id
		"DeptId", -- 隶属部门的 Id
		"OrgId", -- 根组织的 Id
		"CreateTime", -- 分配时间
		"Description"  -- 描述
	from
		"waln_authority"."UserPosts"
	where
		"PK" = @pk;

	insert into "waln_authority"."UserPosts"
	(
		"UserId", -- 用户的 Id
		"PostId", -- 工作岗位的 Id
		"DeptId", -- 隶属部门的 Id
		"OrgId", -- 根组织的 Id
		"CreateTime", -- 分配时间
		"Description"  -- 描述
	)
	values
	(
		@userId, -- 用户的 Id
		@postId, -- 工作岗位的 Id
		@deptId, -- 隶属部门的 Id
		@orgId, -- 根组织的 Id
		@createTime, -- 分配时间
		@description  -- 描述
	);

	update
		"waln_authority"."UserPosts"
	set
		"UserId" = @userId, -- 用户的 Id
		"PostId" = @postId, -- 工作岗位的 Id
		"DeptId" = @deptId, -- 隶属部门的 Id
		"OrgId" = @orgId, -- 根组织的 Id
		"CreateTime" = @createTime, -- 分配时间
		"Description" = @description  -- 描述
	where
		"PK" = @pk;

	delete from
		"waln_authority"."UserPosts"
	where
		"PK" = @pk;
*/

/*
 * # 数据库表 - Sql Server:
 *  Permissions - 权限
 * 
 * # 描述:
 *  定义组织、部门、工作岗位或用户对功能模块的权限。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_authority"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_authority"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_authority"."_pittypat_table_exists"('"Permissions"') = 0
	begin
		create table "waln_authority"."Permissions"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"ModuleId" uniqueidentifier not null, -- 模块的 Id
			"RoleId" uniqueidentifier not null, -- 权利对象的 Id
			"RoleType" tinyint not null, -- 权利人类型代码
			"GrantTime" datetime2 not null, -- 授权时间
			"ExpireTime" datetime2 null -- 有效截止时间
		)
		on "primary";

		-- 索引 K_Permissions
		alter table "waln_authority"."Permissions" add constraint
		"K_Permissions" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Permissions_ModuleId
		create nonclustered index "I_Permissions_ModuleId"
		on "waln_authority"."Permissions"
		(
			"ModuleId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Permissions_Role
		create nonclustered index "I_Permissions_Role"
		on "waln_authority"."Permissions"
		(
			"RoleType" asc,
			"RoleId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 ModuleId 
		if "waln_authority"."_pittypat_column_exists"('Permissions', 'ModuleId') = 0
			alter table "waln_authority"."Permissions" add
			"ModuleId" uniqueidentifier not null;
		else
			alter table "waln_authority"."Permissions" alter column
			"ModuleId" uniqueidentifier not null;

		-- 列 RoleId 
		if "waln_authority"."_pittypat_column_exists"('Permissions', 'RoleId') = 0
			alter table "waln_authority"."Permissions" add
			"RoleId" uniqueidentifier not null;
		else
			alter table "waln_authority"."Permissions" alter column
			"RoleId" uniqueidentifier not null;

		-- 列 RoleType 
		if "waln_authority"."_pittypat_column_exists"('Permissions', 'RoleType') = 0
			alter table "waln_authority"."Permissions" add
			"RoleType" tinyint not null;
		else
			alter table "waln_authority"."Permissions" alter column
			"RoleType" tinyint not null;

		-- 列 GrantTime 
		if "waln_authority"."_pittypat_column_exists"('Permissions', 'GrantTime') = 0
			alter table "waln_authority"."Permissions" add
			"GrantTime" datetime2 not null;
		else
			alter table "waln_authority"."Permissions" alter column
			"GrantTime" datetime2 not null;

		-- 列 ExpireTime 
		if "waln_authority"."_pittypat_column_exists"('Permissions', 'ExpireTime') = 0
			alter table "waln_authority"."Permissions" add
			"ExpireTime" datetime2 null;
		else
			alter table "waln_authority"."Permissions" alter column
			"ExpireTime" datetime2 null;

		-- 删除索引 I_Permissions_ModuleId
		if "waln_authority"."_pittypat_index_exists"('Permissions', 'I_Permissions_ModuleId') = 1
			drop index "I_Permissions_ModuleId" on "waln_authority"."Permissions";

		-- 创建索引 I_Permissions_ModuleId
		create nonclustered index "I_Permissions_ModuleId"
		on "waln_authority"."Permissions"
		(
			"ModuleId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Permissions_Role
		if "waln_authority"."_pittypat_index_exists"('Permissions', 'I_Permissions_Role') = 1
			drop index "I_Permissions_Role" on "waln_authority"."Permissions";

		-- 创建索引 I_Permissions_Role
		create nonclustered index "I_Permissions_Role"
		on "waln_authority"."Permissions"
		(
			"RoleType" asc,
			"RoleId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_authority"."_pittypat_replace_table";
go

/*
	select
		"ModuleId", -- 模块的 Id
		"RoleId", -- 权利对象的 Id
		"RoleType", -- 权利人类型代码
		"GrantTime", -- 授权时间
		"ExpireTime"  -- 有效截止时间
	from
		"waln_authority"."Permissions"
	where
		"PK" = @pk;

	insert into "waln_authority"."Permissions"
	(
		"ModuleId", -- 模块的 Id
		"RoleId", -- 权利对象的 Id
		"RoleType", -- 权利人类型代码
		"GrantTime", -- 授权时间
		"ExpireTime"  -- 有效截止时间
	)
	values
	(
		@moduleId, -- 模块的 Id
		@roleId, -- 权利对象的 Id
		@roleType, -- 权利人类型代码
		@grantTime, -- 授权时间
		@expireTime  -- 有效截止时间
	);

	update
		"waln_authority"."Permissions"
	set
		"ModuleId" = @moduleId, -- 模块的 Id
		"RoleId" = @roleId, -- 权利对象的 Id
		"RoleType" = @roleType, -- 权利人类型代码
		"GrantTime" = @grantTime, -- 授权时间
		"ExpireTime" = @expireTime  -- 有效截止时间
	where
		"PK" = @pk;

	delete from
		"waln_authority"."Permissions"
	where
		"PK" = @pk;
*/

/*
 * # 数据库表 - Sql Server:
 *  Apps - App
 * 
 * # 描述:
 *  已经安装的 App。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_authority"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_authority"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_authority"."_pittypat_table_exists"('"Apps"') = 0
	begin
		create table "waln_authority"."Apps"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"Id" uniqueidentifier not null, -- Id
			"DeveloperCode" varchar(30) not null, -- 开发者代码
			"DeveloperName" nvarchar(50) not null, -- 开发者名称
			"Identifier" varchar(30) not null, -- App 标识符
			"Name" nvarchar(50) not null, -- App 名称
			"Version" varchar(24) not null, -- App 版本
			"ModuleCount" smallint not null, -- 模块数目
			"Description" nvarchar(2000) null, -- 描述
			"Position" int not null, -- 位置
			"InstallTime" datetime2 not null -- 安装时间
		)
		on "primary";

		-- 索引 K_Apps
		alter table "waln_authority"."Apps" add constraint
		"K_Apps" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Apps_Id
		create unique nonclustered index "U_Apps_Id"
		on "waln_authority"."Apps"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Apps_Path
		create unique nonclustered index "U_Apps_Path"
		on "waln_authority"."Apps"
		(
			"DeveloperCode" asc,
			"Identifier" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 Id 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'Id') = 0
			alter table "waln_authority"."Apps" add
			"Id" uniqueidentifier not null;
		else
			alter table "waln_authority"."Apps" alter column
			"Id" uniqueidentifier not null;

		-- 列 DeveloperCode 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'DeveloperCode') = 0
			alter table "waln_authority"."Apps" add
			"DeveloperCode" varchar(30) not null;
		else
			alter table "waln_authority"."Apps" alter column
			"DeveloperCode" varchar(30) not null;

		-- 列 DeveloperName 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'DeveloperName') = 0
			alter table "waln_authority"."Apps" add
			"DeveloperName" nvarchar(50) not null;
		else
			alter table "waln_authority"."Apps" alter column
			"DeveloperName" nvarchar(50) not null;

		-- 列 Identifier 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'Identifier') = 0
			alter table "waln_authority"."Apps" add
			"Identifier" varchar(30) not null;
		else
			alter table "waln_authority"."Apps" alter column
			"Identifier" varchar(30) not null;

		-- 列 Name 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'Name') = 0
			alter table "waln_authority"."Apps" add
			"Name" nvarchar(50) not null;
		else
			alter table "waln_authority"."Apps" alter column
			"Name" nvarchar(50) not null;

		-- 列 Version 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'Version') = 0
			alter table "waln_authority"."Apps" add
			"Version" varchar(24) not null;
		else
			alter table "waln_authority"."Apps" alter column
			"Version" varchar(24) not null;

		-- 列 ModuleCount 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'ModuleCount') = 0
			alter table "waln_authority"."Apps" add
			"ModuleCount" smallint not null;
		else
			alter table "waln_authority"."Apps" alter column
			"ModuleCount" smallint not null;

		-- 列 Description 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'Description') = 0
			alter table "waln_authority"."Apps" add
			"Description" nvarchar(2000) null;
		else
			alter table "waln_authority"."Apps" alter column
			"Description" nvarchar(2000) null;

		-- 列 Position 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'Position') = 0
			alter table "waln_authority"."Apps" add
			"Position" int not null;
		else
			alter table "waln_authority"."Apps" alter column
			"Position" int not null;

		-- 列 InstallTime 
		if "waln_authority"."_pittypat_column_exists"('Apps', 'InstallTime') = 0
			alter table "waln_authority"."Apps" add
			"InstallTime" datetime2 not null;
		else
			alter table "waln_authority"."Apps" alter column
			"InstallTime" datetime2 not null;

		-- 删除索引 U_Apps_Id
		if "waln_authority"."_pittypat_index_exists"('Apps', 'U_Apps_Id') = 1
			drop index "U_Apps_Id" on "waln_authority"."Apps";

		-- 创建索引 U_Apps_Id
		create unique nonclustered index "U_Apps_Id"
		on "waln_authority"."Apps"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 U_Apps_Path
		if "waln_authority"."_pittypat_index_exists"('Apps', 'U_Apps_Path') = 1
			drop index "U_Apps_Path" on "waln_authority"."Apps";

		-- 创建索引 U_Apps_Path
		create unique nonclustered index "U_Apps_Path"
		on "waln_authority"."Apps"
		(
			"DeveloperCode" asc,
			"Identifier" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_authority"."_pittypat_replace_table";
go

/*
	select
		"Id", -- Id
		"DeveloperCode", -- 开发者代码
		"DeveloperName", -- 开发者名称
		"Identifier", -- App 标识符
		"Name", -- App 名称
		"Version", -- App 版本
		"ModuleCount", -- 模块数目
		"Description", -- 描述
		"Position", -- 位置
		"InstallTime"  -- 安装时间
	from
		"waln_authority"."Apps"
	where
		"Id" = @id;

	insert into "waln_authority"."Apps"
	(
		"Id", -- Id
		"DeveloperCode", -- 开发者代码
		"DeveloperName", -- 开发者名称
		"Identifier", -- App 标识符
		"Name", -- App 名称
		"Version", -- App 版本
		"ModuleCount", -- 模块数目
		"Description", -- 描述
		"Position", -- 位置
		"InstallTime"  -- 安装时间
	)
	values
	(
		@id, -- Id
		@developerCode, -- 开发者代码
		@developerName, -- 开发者名称
		@identifier, -- App 标识符
		@name, -- App 名称
		@version, -- App 版本
		@moduleCount, -- 模块数目
		@description, -- 描述
		@position, -- 位置
		@installTime  -- 安装时间
	);

	update
		"waln_authority"."Apps"
	set
		"Id" = @id, -- Id
		"DeveloperCode" = @developerCode, -- 开发者代码
		"DeveloperName" = @developerName, -- 开发者名称
		"Identifier" = @identifier, -- App 标识符
		"Name" = @name, -- App 名称
		"Version" = @version, -- App 版本
		"ModuleCount" = @moduleCount, -- 模块数目
		"Description" = @description, -- 描述
		"Position" = @position, -- 位置
		"InstallTime" = @installTime  -- 安装时间
	where
		"Id" = @id;

	delete from
		"waln_authority"."Apps"
	where
		"Id" = @id;
*/

/*
 * # 数据库表 - Sql Server:
 *  Modules - 模块
 * 
 * # 描述:
 *  表示 App 中的一个模块。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_authority"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_authority"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_authority"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_authority"."_pittypat_table_exists"('"Modules"') = 0
	begin
		create table "waln_authority"."Modules"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"Id" uniqueidentifier not null, -- Id
			"AppId" uniqueidentifier not null, -- 隶属 App 的 Id
			"Position" smallint not null, -- 位置
			"Identifier" nvarchar(30) not null, -- 标识符
			"Name" nvarchar(50) not null, -- 模块名称
			"Presentation" tinyint not null, -- 呈现方式
			"AllowAnonymous" bit not null, -- 允许匿名访问
			"Description" nvarchar(2000) null -- 描述
		)
		on "primary";

		-- 索引 K_Modules
		alter table "waln_authority"."Modules" add constraint
		"K_Modules" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Modules_Id
		create unique nonclustered index "U_Modules_Id"
		on "waln_authority"."Modules"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Modules_AppId
		create nonclustered index "I_Modules_AppId"
		on "waln_authority"."Modules"
		(
			"AppId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 Id 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'Id') = 0
			alter table "waln_authority"."Modules" add
			"Id" uniqueidentifier not null;
		else
			alter table "waln_authority"."Modules" alter column
			"Id" uniqueidentifier not null;

		-- 列 AppId 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'AppId') = 0
			alter table "waln_authority"."Modules" add
			"AppId" uniqueidentifier not null;
		else
			alter table "waln_authority"."Modules" alter column
			"AppId" uniqueidentifier not null;

		-- 列 Position 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'Position') = 0
			alter table "waln_authority"."Modules" add
			"Position" smallint not null;
		else
			alter table "waln_authority"."Modules" alter column
			"Position" smallint not null;

		-- 列 Identifier 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'Identifier') = 0
			alter table "waln_authority"."Modules" add
			"Identifier" nvarchar(30) not null;
		else
			alter table "waln_authority"."Modules" alter column
			"Identifier" nvarchar(30) not null;

		-- 列 Name 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'Name') = 0
			alter table "waln_authority"."Modules" add
			"Name" nvarchar(50) not null;
		else
			alter table "waln_authority"."Modules" alter column
			"Name" nvarchar(50) not null;

		-- 列 Presentation 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'Presentation') = 0
			alter table "waln_authority"."Modules" add
			"Presentation" tinyint not null;
		else
			alter table "waln_authority"."Modules" alter column
			"Presentation" tinyint not null;

		-- 列 AllowAnonymous 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'AllowAnonymous') = 0
			alter table "waln_authority"."Modules" add
			"AllowAnonymous" bit not null;
		else
			alter table "waln_authority"."Modules" alter column
			"AllowAnonymous" bit not null;

		-- 列 Description 
		if "waln_authority"."_pittypat_column_exists"('Modules', 'Description') = 0
			alter table "waln_authority"."Modules" add
			"Description" nvarchar(2000) null;
		else
			alter table "waln_authority"."Modules" alter column
			"Description" nvarchar(2000) null;

		-- 删除索引 U_Modules_Id
		if "waln_authority"."_pittypat_index_exists"('Modules', 'U_Modules_Id') = 1
			drop index "U_Modules_Id" on "waln_authority"."Modules";

		-- 创建索引 U_Modules_Id
		create unique nonclustered index "U_Modules_Id"
		on "waln_authority"."Modules"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Modules_AppId
		if "waln_authority"."_pittypat_index_exists"('Modules', 'I_Modules_AppId') = 1
			drop index "I_Modules_AppId" on "waln_authority"."Modules";

		-- 创建索引 I_Modules_AppId
		create nonclustered index "I_Modules_AppId"
		on "waln_authority"."Modules"
		(
			"AppId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_authority"."_pittypat_replace_table";
go

/*
	select
		"Id", -- Id
		"AppId", -- 隶属 App 的 Id
		"Position", -- 位置
		"Identifier", -- 标识符
		"Name", -- 模块名称
		"Presentation", -- 呈现方式
		"AllowAnonymous", -- 允许匿名访问
		"Description"  -- 描述
	from
		"waln_authority"."Modules"
	where
		"Id" = @id;

	insert into "waln_authority"."Modules"
	(
		"Id", -- Id
		"AppId", -- 隶属 App 的 Id
		"Position", -- 位置
		"Identifier", -- 标识符
		"Name", -- 模块名称
		"Presentation", -- 呈现方式
		"AllowAnonymous", -- 允许匿名访问
		"Description"  -- 描述
	)
	values
	(
		@id, -- Id
		@appId, -- 隶属 App 的 Id
		@position, -- 位置
		@identifier, -- 标识符
		@name, -- 模块名称
		@presentation, -- 呈现方式
		@allowAnonymous, -- 允许匿名访问
		@description  -- 描述
	);

	update
		"waln_authority"."Modules"
	set
		"Id" = @id, -- Id
		"AppId" = @appId, -- 隶属 App 的 Id
		"Position" = @position, -- 位置
		"Identifier" = @identifier, -- 标识符
		"Name" = @name, -- 模块名称
		"Presentation" = @presentation, -- 呈现方式
		"AllowAnonymous" = @allowAnonymous, -- 允许匿名访问
		"Description" = @description  -- 描述
	where
		"Id" = @id;

	delete from
		"waln_authority"."Modules"
	where
		"Id" = @id;
*/
